package com.handpay.zztong.hp.webpay.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.MD5;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.swiper.activity.VPOSActivity;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

import java.math.BigDecimal;
import java.util.Hashtable;

import static com.handpay.zztong.hp.R.string.capture_title_merchant;

/**
 * @author sxshi
 * @describe:新版秒秒通收款二维码收款界面
 */
public class QRCodeReceiptActivity extends ZZTongActivity implements OnClickListener {
    private ImageView btn_scan;
    private ImageView qrcode_iv;
    private Bitmap mQRCodeBitmap;
    private double amount;//支付金额
    private TextView amount_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_qrcode_receipt);
        super.onCreate(savedInstanceState);
        qrcode_iv = (ImageView) this.findViewById(R.id.qrcode_iv);
        btn_scan = (ImageView) this.findViewById(R.id.btn_scan);
        amount_tv = (TextView) findViewById(R.id.amount_tv);
        btn_scan.setOnClickListener(this);
        BitmapDrawable ic_launcher = (BitmapDrawable) this.getResources().getDrawable(R.drawable.ic_launcher);
        String host;
        if (ZZTConfig.isTesting) {
            host = "https://app.handbank.cn/hpayMicroWap/scanpay/index.htm";
        } else {
//            host = "https://wallet.handpay.cn/hpayJHSupport/scan2/wap/enter.htm";
            host = "https://scan.handpay.cn/hpayMicroWap/scanpay/index.htm";
        }
        String url;
        String merchantCode = AccountUtils.getMerchantCode(this);
        String secretkey = "HaN_yIn_scan_Pay";
        myActionBar.refreshActionBar(new ActionBar(this.getString(capture_title_merchant), true));
        amount = this.getIntent().getDoubleExtra(VPOSActivity.Transfer_Amount_Key, 0d);
        String amountStr = String.format("¥%s", CommonUtils.formatMoneyWithSplite(amount / 100.00));
        SpannableStringBuilder ssb = new SpannableStringBuilder(amountStr);
        ssb.setSpan(new RelativeSizeSpan(1.5f), 1, ssb.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        amount_tv.setText(ssb);
        String sign = new MD5().getMD5ofStr(merchantCode + BigDecimal.valueOf(amount).intValue() + secretkey).toLowerCase();
        url = host + "?channel=" + ClientEngine.CHANNEL + "&merchantCode=" + merchantCode + "&amount=" + BigDecimal.valueOf(amount).intValue() + "&clientChannel=" + ZZTConfig.UPDATE_CHANNEL + "&clientVersion=v" + ClientEngine.VERSION + "&sign=" + sign;
        HPLog.i(HPLog.YL_TAG, "load url:" + url);
        mQRCodeBitmap = EncodingUtils.createQRCode(url, CommonUtils.dp2px(300), CommonUtils.dp2px(300), ic_launcher.getBitmap());
        qrcode_iv.setImageBitmap(mQRCodeBitmap);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_scan) {
            Hashtable<String, Object> params = new Hashtable<String, Object>();
            params.put(ZZTongActivity.ACTION, ZZTongActivity.Action_Transfer1);
            params.put(VPOSActivity.Transfer_Amount_Key, amount);
            startActivity(QRCodeReceiptActivity.this, WebPayWaitingResultActivity.class, params);
            finish();
            overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
        }
    }

    @Override
    protected boolean isShownTopBar() {
        return true;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.capture_title_merchant), true);
    }

}
